# Copyright (C) 2023 Advanced Micro Devices, Inc.  All rights reserved.
# Copyright (c) 2021 Xilinx, Inc.  All rights reserved.
# SPDX-License-Identifier: MIT
cmake_minimum_required(VERSION 3.15)
project(sysmonpsv)

find_package(common)
set(SYSMONPSV_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
find_package(SYSMONPSV)
list(APPEND defs "#ifndef XSYSMONPSV_SUPPLYLIST\n")
list(APPEND defs "#define XSYSMONPSV_SUPPLYLIST\n\n")
if ("${DRIVER_OPTPROP_0_LIST}" STREQUAL "")
    list(APPEND defs "#define XPAR_XSYSMONPSV_0_NO_MEAS       255\n")
endif()
list(APPEND defs "\n/*\n* The supply configuration table for sysmon\n*/\n")
list(APPEND defs "typedef enum {\n")
if (DRIVER_OPTPROP_0_LIST)
    foreach(PROP ${DRIVER_OPTPROP_0_LIST})
        string(TOUPPER ${PROP} PROP_UPPER)
        list(APPEND defs "\t${PROP_UPPER},\n")
    endforeach()
    list(APPEND defs "\tEndList,\n")
else()
    list(APPEND defs "\tEndList,\n")
    list(APPEND defs "\tNO_SUPPLIES_CONFIGURED = XPAR_XSYSMONPSV_0_NO_MEAS,\n")
endif()
list(APPEND defs "} XSysMonPsv_Supply;\\")
list(APPEND defs "\n\n#endif")
file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/xsysmonpsv_supplylist.h ${defs})

file(READ ${CMAKE_CURRENT_SOURCE_DIR}/xsysmonpsv_g.c defs)
string(FIND "${defs}" "XSysMonPsv_Supply_Arr" _index)
if (${_index} EQUAL -1)
    list(APPEND defs ";\\")
    list(APPEND defs "\n\nconst char *XSysMonPsv_Supply_Arr[] = {\n")
    if (DRIVER_OPTPROP_0_LIST)
        foreach(PROP ${DRIVER_OPTPROP_0_LIST})
            string(TOUPPER ${PROP} PROP_UPPER)
            list(APPEND defs "\t\"${PROP_UPPER}\",\n")
        endforeach()
    endif()
    list(APPEND defs "};\\")
    list(APPEND defs "\n")
    file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/xsysmonpsv_g.c ${defs})
endif()

collector_create (PROJECT_LIB_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}")
collector_create (PROJECT_LIB_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}")
include_directories(${CMAKE_BINARY_DIR}/include)
collect (PROJECT_LIB_SOURCES xsysmonpsv_sinit.c)
collect (PROJECT_LIB_SOURCES xsysmonpsv_g.c)
collect (PROJECT_LIB_SOURCES xsysmonpsv.c)
collect (PROJECT_LIB_HEADERS xsysmonpsv.h)
collect (PROJECT_LIB_HEADERS xsysmonpsv_supplylist.h)
#collect (PROJECT_LIB_HEADERS xsysmonpsv_hw.h)
collect (PROJECT_LIB_SOURCES xsysmonpsv_intr.c)
add_subdirectory(common)
add_subdirectory(lowlevel)
add_subdirectory(services)
collector_list (_sources PROJECT_LIB_SOURCES)
collector_list (_headers PROJECT_LIB_HEADERS)
file(COPY ${_headers} DESTINATION ${CMAKE_BINARY_DIR}/include)
add_library(sysmonpsv STATIC ${_sources})
set_target_properties(sysmonpsv PROPERTIES LINKER_LANGUAGE C)
